## ANTES DE USAR
# Para criar data/movies.csv
# import_data("tom_cruise") ou com o ator/atriz que você escolher
#import_data("harrison_ford")
#import_data("brad_pitt")
import_data("robert_de_niro")
## Loading required package: rvest
## Loading required package: xml2
## 
## Attaching package: 'rvest'
## The following object is masked from 'package:purrr':
## 
##     pluck
## The following object is masked from 'package:readr':
## 
##     guess_encoding
## Warning in evalq(as.numeric(gsub("[$|M]", "", `BOX OFFICE`)),
## <environment>): NAs introduced by coercion
filmes = read_imported_data()

Descrição

filmes %>% 
    ggplot(aes(x = ano, y = bilheteria)) + 
    geom_point(size = 4, color = paleta[1]) 

filmes %>% 
    ggplot(aes(x = bilheteria)) + 
    geom_histogram(binwidth = 15, fill = paleta[2], color = "black") + 
    geom_rug(size = .5) 

filmes %>% 
    ggplot(aes(x = avaliacao)) + 
    geom_histogram(binwidth = 10, boundary = 0, fill = paleta[3], color = "black") + 
    geom_rug(size = .5) 

Estrutura de grupos?

p = filmes %>% 
    ggplot(aes(x = "", y = bilheteria, label = filme)) + 
    geom_jitter(width = .05, alpha = .3, size = 3) + 
    labs(x = "")

ggplotly(p)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`

Agrupamento hierárquico

agrupamento_h = filmes %>% 
    mutate(nome = paste0(filme, " (av=", avaliacao, ")")) %>% 
    as.data.frame() %>% 
    column_to_rownames("filme") %>% 
    select(avaliacao) %>%
    dist(method = "euclidian") %>% 
    hclust(method = "ward.D")

agrupamento_h_2d = filmes %>% 
    mutate(bilheteria = log10(bilheteria)) %>%
    mutate_at(vars(avaliacao, bilheteria), 
              funs(scale)) %>% 
    as.data.frame() %>%
    column_to_rownames("filme") %>%
    select(avaliacao, bilheteria) %>%
    dist(method = "euclidean") %>% 
    hclust(method = "ward.D")

ggdendrogram(agrupamento_h, rotate = T, size = 2, theme_dendro = F) + 
    labs(y = "Dissimilaridade", x = "", title = "Dendrograma")

get_grupos <- function(agrupamento, num_grupos){
    agrupamento %>% 
        cutree(num_grupos) %>% 
        as.data.frame() %>% 
        mutate(label = rownames(.)) %>% 
        gather(key =  "k", value = "grupo", -label) %>% 
        mutate(grupo = as.character(grupo))
}

atribuicoes = get_grupos(agrupamento_h_2d, num_grupos = 1:6)

atribuicoes = atribuicoes %>% 
    left_join(filmes, by = c("label" = "filme"))

atribuicoes %>% 
    mutate(bilheteria = log10(bilheteria)) %>%
    ggplot(aes(x = bilheteria, y = avaliacao, colour = grupo)) + 
    geom_jitter(width = .02, height = 0, size = 1.6, alpha = .6) + 
    facet_wrap(~ paste(k, " grupos")) + 
    scale_color_brewer(palette = "Dark2")

k_escolhido = 4

p <- atribuicoes %>% 
    filter(k == k_escolhido) %>% 
    ggplot(aes(x = log10(bilheteria), y = avaliacao, colour = grupo, label = label)) + 
    geom_jitter(width = .02, height = 0, size = 3, alpha = .6) + 
    facet_wrap(~ paste(k, " tipos de filmes de Robert De Niro")) + 
    scale_color_brewer(palette = "Dark2") + 
    labs(x = "Bilheteria", y = "Avaliação") + 
    coord_flip() + 
    theme(legend.position = "none")

ggplotly(p)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`

Podemos ver 4 tipos de filmes de Robert De Niro ao longo de sua carreira:

  1. Totais fracassos de de crítica com bilheteria razoável.
  2. Totais fracassos de bilheteria e crítica razoável.
  3. Crítica razoável e boa bilheteria.
  4. Boa crítica e bilheteria.